package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.UserMapper;
import com.heima.pojo.dto.UserQueryDTO;
import com.heima.pojo.entity.TbRole;
import com.heima.pojo.entity.TbUser;
import com.heima.pojo.entity.User;
import com.heima.pojo.dto.LoginDto;
import com.heima.pojo.vo.LoginUserVo;
import com.heima.pojo.vo.TbSkuPageVO;
import com.heima.pojo.vo.UserRoleVO;
import com.heima.pojo.vo.VendingMachineVO;
import com.heima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null){
            //参数异常
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName,dto.getLoginName());
        User user = userMapper.selectOne(wrapper);

        if(user == null){ //代表用户不存在得

        }
        if(!user.getPassword().equals(dto.getPassword())){ //代表密码错误

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    @Override
    public TbSkuPageVO<UserRoleVO> search(UserQueryDTO dto) {
        // 计算分页偏移量
        int offset = (dto.getPageIndex() - 1) * dto.getPageSize();
        // 创建新的DTO对象用于传递偏移量
        dto.setOffset(offset);
        
        TbSkuPageVO<UserRoleVO> tbSkuPageVO = new TbSkuPageVO<>();
        List<UserRoleVO> records = userMapper.search(dto);
        
        // 手动设置分页信息
        tbSkuPageVO.setPageIndex(Long.valueOf(dto.getPageIndex()));
        tbSkuPageVO.setPageSize(Long.valueOf(dto.getPageSize()));
        // 由于我们没有使用PageHelper，需要手动查询总数
        long totalCount = userMapper.count(dto);
        tbSkuPageVO.setTotalCount(totalCount);
        tbSkuPageVO.setTotalPage((totalCount + dto.getPageSize() - 1) / dto.getPageSize());
        tbSkuPageVO.setCurrentPageRecords(records);
        return tbSkuPageVO;
    }
    
    @Override
    public List<TbRole> insertRole() {
        return userMapper.insertRole();
    }

    @Override
    public Boolean insertUser(TbUser tbUser) {
        try {
            return userMapper.insertUser(tbUser) > 0;
        } catch (Exception e) {
            log.error("新增用户失败，tbUser={}", tbUser, e);
        }
        return false;

    }

    @Override
    public Boolean updateUser(Long userId, TbUser tbUser) {
        try {

            return userMapper.updateUser(userId, tbUser) > 0;
        } catch (Exception e) {
            log.error("更新用户失败，userId={}, tbUser={}", userId, tbUser, e);
        }
        return false;
    }

    @Override
    public Boolean deleteUser(Long userId) {
        try {
            return userMapper.deleteUser(userId) > 0;
        } catch (Exception e) {
            log.error("删除用户失败，userId={}", userId, e);
        }
        return false;
    }
}